<?php

class LoadViewDiagrams {

    /**
     * Draws all necessary diagrams
     * 
     * @param String $load              sudivision's or IS data
     * @param String $sort_by           quantities or time
     * @param int    $items_in_width    items to show not devided.
     *                                  <code>null</code> - to show all
     */
    public static function draw($load, $sort_by, $items_in_width = null, $width = 1000) {
        /* Content */
        require 'Processing/Subdivisions/subdivision_data_processing.php';
        $dataset = SubdivisionData::counts_to_dataset();
        $subdivisions = get_subdivisions_associative();
        $measures = get_support_measures_associative();
        $dates = SubdivisionData::get_dates_predicted(12);
        $time_groups = SubdivisionData::dataset_to_table($dataset, $subdivisions, $dates, $is_time_groups);
        require 'LoadViewDesign.php';
        $margin = 40 * 2;
        
        
        /* Caching */
        $cache_date = get_load_last_modified();
        $width -= $margin;

        if (count($dates) < 1 || count($time_groups) < 1) {
            echo "Duomenų apie istorinius kiekius nėra. Pirma importuokite juos.";
            return false;
        }
        
        /* Subdivisions */
        if ($load == 'subdivision') {
            /* Dividing for printable verrsion */
            $divided_dates = SubdivisionData::divide_dates($dates, $items_in_width, $time_groups, $new_time_groups);
            $n = count($divided_dates);
            if ($items_in_width) {
                $step_size = $width / $items_in_width;
                $table_width = $width;
            } else {
                $step_size = $width / count($dates);
                $table_width = null;
            }
            
            SubdivisionData::predict($time_groups);
            if ($sort_by == 'time') {
                MainDesign::h1('Padalinių apkrova pagal sugaištą laiką');

                MainDesign::viewSectionBegin('Taškinė apžvalgos diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::dotted_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Linijinė palyginamoji diagrama', true);
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        if ($items_in_width === null) {
                            $file = "sub_%MODE%t.gif";
                        } else {
                            $file = "sub_%MODE%t_sl_$i.gif";
                        }
                        LoadViewDesign::php_linear_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $cache_date, $current_width, 300, $file, $last);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Apkrovos lentelė (sugaištas laikas)');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                    }
                    
                MainDesign::viewSectionEnd();
            } else {
                MainDesign::h1('Padalinių apkrova pagal paraiškų skaičių');

                /* Diagrams */
                MainDesign::viewSectionBegin('Taškinė apžvalgos diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::dotted_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Linijinė palyginamoji diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        if ($items_in_width === null) {
                            $file = "sub_%MODE%q.gif";
                        } else {
                            $file = "sub_%MODE%q_sl_$i.gif";
                        }
                        LoadViewDesign::php_linear_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $cache_date, $current_width, 300, $file, $last);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Apkrovos lentelė (suadministruotų paraiškų kiekiai)');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::table_sub($new_dates, $subdivisions, $new_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                    }
                MainDesign::viewSectionEnd();
            }

            
            
            /* Information system */
        } else {
            require 'Processing/InformationSystems/is_data_processing.php';
            $is_names = get_is_associative();
            ISData::predict_is($is_time_groups);
            
            /* Dividing for printable verrsion */
            $divided_dates = SubdivisionData::divide_dates($dates, $items_in_width, $is_time_groups, $new_is_time_groups);
            $n = count($divided_dates);
            if ($items_in_width) {
                $step_size = $width / $items_in_width;
                $table_width = $width;
            } else {
                $step_size = $width / count($dates);
                $table_width = null;
            }

            if ($sort_by == 'time') {
                MainDesign::h1('Informacinių sistemų apkrova pagal sugaištą laiką');

                MainDesign::viewSectionBegin('Taškinė apžvalgos diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::dotted_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Linijinė palyginamoji diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        if ($items_in_width === null) {
                            $file = "is_%MODE%t.gif";
                        } else {
                            $file = "is_%MODE%t_sl_$i.gif";
                        }
                        LoadViewDesign::php_linear_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $cache_date, $current_width, 300, $file, $last);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Apkrovos lentelė (sugaištas laikas)');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::table_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_TIME, $table_width);
                    }
                MainDesign::viewSectionEnd();
            } else {
                MainDesign::h1('Informacinių sistemų apkrova pagal paraiškų skaičių');

                MainDesign::viewSectionBegin('Taškinė apžvalgos diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::dotted_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Linijinė palyginamoji diagrama');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        if ($items_in_width === null) {
                            $file = "is_%MODE%q.gif";;
                        } else {
                            $file = "is_%MODE%q_sl_$i.gif";
                        }
                        LoadViewDesign::php_linear_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $cache_date, $current_width, 300, $file, $last);
                    }
                MainDesign::viewSectionEnd();

                MainDesign::viewSectionBegin('Apkrovos lentelė (suadministruotų paraiškų kiekiai)');
                    foreach ($divided_dates as $i=>$new_dates) {
                        $current_width = floor($step_size * count($new_dates)) + $margin;
                        $last = ($i == $n - 1);
                        LoadViewDesign::table_is($dates, $is_names, $new_is_time_groups[$i], LoadViewDesign::DIAGRAM_MODE_ADMINISTRATED, $table_width);
                    }
                MainDesign::viewSectionEnd();
            }
        }
        ?>
        <br/>
        <div id="loadViewInfo"><span class="predicted">Tamsesniame fone</span> rodoma prognozuojama apkrova.</div>
        <?
        return true;
    }
}